
from PyPDF2 import PdfFileReader, PdfFileMerger


def merge_pdfs(paths, new_file_name):
    #用Merge生成一个空的PDF文件
    merger = PdfFileMerger()
    #记录当前总页数，后面要用到
    page_nums = 0

    for path in paths:
        pdf = PdfFileReader(path)
        #把pdf合并到merge中去，第一个参数指定插入位置
        merger.merge(page_nums, pdf)
        #增加总页数
        page_nums += pdf.numPages

    #把merger写入到文件中
    with open(new_file_name, 'wb') as f:
        merger.write(f)

if __name__ == '__main__':
    paths = ['dog_1.pdf', 'dog_2.pdf']
    merge_pdfs(paths, 'merged_dogs.pdf')
